/**
 * Methods for file upload
*/
 function createDialogForFileUpload() {
	var titleStr =arguments[0];
	var infoUrl=arguments[1];
	var postUrl=arguments[2];
	var callback=arguments[3];
	var actionType=arguments[3];
	
	$.getJSON(infoUrl,function(components,status){
		
		 var dialogObj=$('<div id="FileUploadDialog"></div>');
	     
		 //Add form object to DIV
	     var formObject =$('<form method="post"/>');
	     $(formObject).appendTo(dialogObj);
	     
	     
	     //Add table to Form
	     var tableObj=$('<table></table>');
	     $(tableObj).appendTo(formObject);
	     
	     //Add tbody section to table
	     var tbodyObj =$('<tbody></tbody>');
	     $(tbodyObj).appendTo(tableObj);
	     
	     for(var counter=0;counter<components.length;counter++){
			var compId=components[counter].id;
			var complabel=components[counter].label;
			var compType=components[counter].type;
			var compValue=(components[counter].value==undefined)?"":components[counter].value;
			
			var tableRowObj=$("<tr></tr>");
			var labelObj =$('<td style="vertical-align:top">'+complabel+'</td>');
			$(labelObj).appendTo(tableRowObj);
			
			
			var formElementCell=$('<td></td>');
			var formInputObj=null;
			if(compType == "input_text"){//Input type text
				formInputObj =$('<input type="text" id="'+compId+'" name="'+compId+'" value="'+compValue+'"/>');
			}else if(compType == "input_file"){//File upload String
				formInputObj=$('<input type="file" name="'+compId+'" id="'+compId+'"/>');
				var actionTypeObj=$('<input type="hidden"  name="action" value="'+actionType+'"></input>');
				 $(actionTypeObj).appendTo(formObject);
				isFileUpload=true;
     		}else if(compType == "input_password"){
     			formInputObj =$('<input type="password" id="'+compId+'" name="'+compId+'" value="'+compValue+'"/>');
			}else if(compType == "input_select"){
				formInputObj =$('<select id="'+compId+'" name="'+compId+'"></select>');
				compValue.forEach(function(cValue,index,array){
					$('<option value="'+cValue.op_value+'">'+cValue.op_label+'</option>').appendTo(formInputObj);
				});				
			}else if(compType == "input_radio"){
			}else if(compType == "input_checkbox"){
				formInputObj =$('<input type="checkbox" id="'+compId+'" name="'+compId+'" />');
			}else if(compType == "checkbox_group"){
				var checkBoxObjLen=compValue.length;
				if(checkBoxObjLen >0){
					var formInputObj =$("<table></table>");
					for (var chkBoxCounter=0;chkBoxCounter<checkBoxObjLen;chkBoxCounter++){
						var chkBoxObj=compValue[chkBoxCounter];
						var selectedString=chkBoxObj.checked=="true"?"checked":"";
						var groupObj =$("<tr><td><input type='checkbox' name='"+compId+"' id='"+compId+"' value='"+chkBoxObj.value+"' "+selectedString+">"+chkBoxObj.text+"</td></tr>)";
						$(groupObj).appendTo(formInputObj)
					}
				}			
			}
			
	     	//Add to table cell
			$(formInputObj).appendTo(formElementCell);	
			$(formElementCell).appendTo(tableRowObj);	
			$(tableRowObj).appendTo(tbodyObj);
	     }

		 $(dialogObj).dialog({
				modal: true,
				title: titleStr,
				width: "auto",
				buttons:[ 
							{
								text: "Create",
								click: function() {// Start of click
									if(isFileUpload){
										var iframeObj=$('<iframe />');
                                        $(iframeObj).attr("name","file_upload_frame");
                                        $(iframeObj).attr("id","file_upload_frame");
                                        $(iframeObj).css('display','none');
                                        $(iframeObj).appendTo(dialogObj);
                                        //Fire event when Iframe has finished loading
                                        $(iframeObj).on("load",function(){//Add Event handler to IFrame
                                        	var content=$(iframeObj).contents().find('body').text();
                                            var contentJson=jQuery.parseJSON(content);
                                            if(contentJson.status=="success"){
                                            	jSuccess(contentJson.message);
                                            }else{
                                            	jError(contentJson.message);
                                            }
                                            $(iframeObj).remove();
                                        });
                                        //Set the form object attributes and submit the form
                                        $(formObject).attr('target','file_upload_frame');
                                        $(formObject).attr("action",fileUploadUrl);
                                        $(formObject).attr("enctype","multipart/form-data");
                                        $(formObject).attr("encoding","multipart/form-data");
                                        $(formObject).submit();
                                        dialogObj.dialog('destroy').remove();
									}else {
										var jsonData = {};
										for(var counter=0;counter<components.length;counter++){
											var id=components[counter].id;
											var compIdSelector="#"+id;
											var value="";
											//Check if it is a group of check boxes
											if($(compIdSelector).is(':checkbox')){
												var checkBoxValues = $(compIdSelector+":checked").map(function(){
													return this.value;
												}).toArray();
												value=checkBoxValues;
											}else{//Single element value
												value=$(compIdSelector).val();
											}
											jsonData[id]=value;
										}
										jsonData["action"]="create";
									
										$.post( postUrl+"",jsonData, function( jsonData ) {
												createDialog.dialog('destroy').remove();
												 $('<div class="nowrap" style="width:500px;">Data inserted successfully.</div>').dialog({
												        modal: true,
												        title: "Success",
														width: "auto"
												 }).show();
												 eval (callback);
											})
											.fail(function() {
												createDialog.dialog('destroy').remove();
												 $('<div class="nowrap" style="width:500px;">Error happened while inserting data.</div>').dialog({
												        modal: true,
												        title: "Error",
														width: "auto"
												 }).show();
												
											});
										}
									}//end of click
							},
							{
								text: "Cancel",
								click: function() {
									$( this ).dialog( "close" );
								}
							}
						]
		 }).show();
	}) .fail(function() {
		 $('<div class="nowrap" style="width:500px;">Error happened while fetching data.</div>').dialog({
		        modal: true,
		        title: "Error",
				width: "auto"
		 }).show();
	});
 }
 function updateDialogForFileUpload() {
	 
 }
 function deleteDialogForFileUpload() {
	 
 }